package com.example.signsystem.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.signsystem.entity.dto.AttendanceDTO;

import com.example.signsystem.entity.po.SignListPO;
import com.example.signsystem.entity.po.StudentsPO;
import com.example.signsystem.entity.vo.AttendanceVO;

import com.example.signsystem.mapper.SignListMapper;
import com.example.signsystem.mapper.StudentsMapper;
import com.example.signsystem.service.IAttendanceService;
import com.qcby.framework.common.exception.ServiceException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AttendanceServiceImpl implements IAttendanceService {

    @Resource
    private StudentsMapper studentsMapper;

    @Resource
    private SignListMapper signListMapper;

    @Override
    public AttendanceDTO getAttendanceStatus(String name) {
        AttendanceDTO attendanceDTO=new AttendanceDTO();
        LambdaQueryWrapper<StudentsPO> lqw = new LambdaQueryWrapper<StudentsPO>().select(StudentsPO::getBlogCount)
                .eq(StudentsPO::getName, name);
        StudentsPO studentsPO = studentsMapper.selectOne(lqw);
        attendanceDTO.setBlogCount(studentsPO.getBlogCount());
        Long idByName = getIdByName(name);
        List<SignListPO> signListPOS = signListMapper.selectList(new LambdaQueryWrapper<SignListPO>()
                .eq(SignListPO::getUserId,idByName));

        List<String> signDays = signListPOS.stream().map(SignListPO::getSignDay).collect(Collectors.toList());
        log.info("singListPos:"+signDays);
        if(signDays.isEmpty()){
            throw new ServiceException("500","没有签到信息，请先签到");
        }
        //todo  No format fit for date String
        attendanceDTO.setClassTime(signDays);
        return attendanceDTO;
    }

    /**
     * 通过姓名拿到用户id
     * @param name
     * @return
     */
    public Long getIdByName(String name){
        StudentsPO studentsPO = studentsMapper.selectOne(new LambdaQueryWrapper<StudentsPO>().select(StudentsPO::getId).eq(StudentsPO::getName, name));
        return studentsPO.getId();
    }
}
